package sis.ra.yahooNewsSummary;

import java.io.File;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;


import sis.ra.evaluation.extractVerbOfLaunch;
import sis.ra.utility.InxEntity;
import sis.ra.utility.Utils;

public class assignCompanyName {

	public static ArrayList<InxEntity> parseIMSXMLContentListCompany(String xmlfile)
	{
		ArrayList<InxEntity> ens = new ArrayList<InxEntity>();
		ArrayList<String> names=new ArrayList<String> ();
		Document document = null;
		try {
//			document = new SAXBuilder().build(systemId)
//			System.out.println(xmlfile);
			document = new SAXBuilder().build(new StringReader(xmlfile));
		
		Element root = document.getRootElement();
		Namespace inx = root.getNamespace("inxenv");
		Namespace inx2 = Namespace.getNamespace("http://www.inxight.com/ims/response/4.6");

		Element docprocnode = root.getChild("payload",inx).getChild("document-processing",inx2);
		Element entitylist= docprocnode.getChild("extract-entities",inx2).getChild("entities",inx2);
		List children = entitylist.getChildren("entity",inx2);
		for (Object entity : children) {
			Element el = (Element)entity;
			String canonical=el.getAttributeValue("canonical");
			String canonicaltype=el.getAttributeValue("canonical-type");
			String surface=el.getAttributeValue("surface");
			String entitytype=el.getAttributeValue("entity-type");
			String offset=el.getAttributeValue("offset");
			String confidence=el.getAttributeValue("confidence");
			String relevance=el.getAttributeValue("relevance");
//		InxEntity en=new InxEntity("","", "",surface,entitytype,"");
//		public InxEntity(String canoical, String canoicalType, String confidence,
//		String entityType, String id, String offset, String position,
//		String relevance, String surface) {
			if (entitytype.equals("COMPANY"))//||entitytype.equals("COMPANY"))
			{
				InxEntity en=new InxEntity(canonical, canonicaltype, confidence, entitytype, "", offset, "", relevance, surface);	
				ens.add(en);
			}
		}
		
		} catch (Exception e) {
			System.out.println(xmlfile);
			e.printStackTrace();

		}
		return ens;
	}
	
	public static void writeToEntities()
	{
//		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\testing company\\";//wiki//Amazon.com_wiki.txt";
//		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\yahoo doc\\";
		String filepathstr="C:\\Documents and Settings\\I820753\\Desktop\\dataset\\cnet\\";
		File filepath=new File (filepathstr);
		File[] files=filepath.listFiles();
		for (int i=0;i<files.length;i++)
		{
			if (!files[i].toString().contains(".segsent")) continue;
			File thisfile=new File (files[i].toString().replace(".segsent", ".comp"));
	//		if (thisfile.exists()) continue;
			String content=Utils.readFile(files[i].toString());
			String companyname=files[i].toString().substring(66).replace(".segsent", "");
//			//remove [1]
//			for (int b=1;b<200;b++)
//			content=content.replace("["+b+"]", "");
			
			HashMap<String, String> entityterm=new HashMap<String, String>();
			
			String[] lines=content.split("\n");
			for (int m=0;m<lines.length;m++)
			{
				if (lines[m].length()<1) continue;
				String sentid=lines[m].split("\t")[0];
				String sent=lines[m].split("\t")[1];
				String entityfile=Utils.getNE(sent);
	//		System.out.println(entityfile);
				ArrayList<InxEntity> entities=parseIMSXMLContentListCompany(entityfile);
			
				for (int j=0;j<entities.size();j++)
				{
					if (entityterm.containsKey(entities.get(j).getCanoical()))
					{
						String appear=entityterm.get(entities.get(j).getCanoical());
						appear=appear+" "+companyname.replace(" ", "_")+"-"+sentid+"-"+entities.get(j).getOffset();
						entityterm.remove(entities.get(j).getCanoical());
						entityterm.put(entities.get(j).getCanoical(), appear);
					}else
					{
						String appear=companyname.replace(" ", "_")+"-"+sentid+"-"+entities.get(j).getOffset();
						entityterm.put(entities.get(j).getCanoical(), appear);
					}			
				}
			}
	//		System.out.println(files[i]+"\t"+entityterm.size());
			Iterator<String> its=entityterm.keySet().iterator();
			int l=0;
			String termnow="";
			while (its.hasNext())
			{
				String term=its.next();
	//			System.out.println(term+"\t"+entityterm.get(term));
				int t=entityterm.get(term).split(" ").length;
				if (t>l) 
				{
					termnow=term;
					l=t;
				}
	//			Utils.writeToFile(files[i].toString().replace(".segsent", ".comp"), term+"\t"+entityterm.get(term), true, true);
			}
			System.out.println(files[i]+"\t"+termnow);
	}
	}
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		writeToEntities();
	}

}
